package org.kzwl.service.web.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.kzwl.comm.pojo.ResponseData;
import org.kzwl.comm.pojo.bo.CuVolunteersSignDTO;
import org.kzwl.comm.pojo.bo.VolunteersUserQueryBO;
import org.kzwl.entity.app.CuVolunteersUser;
import org.kzwl.entity.app.User;
import org.kzwl.entity.web.CuVolunteersSign;
import org.kzwl.entity.web.Town;
import org.kzwl.mapper.app.UserMapper;
import org.kzwl.mapper.web.CuVolunteersSignMapper;
import org.kzwl.mapper.web.CuVolunteersUserMapper;
import org.kzwl.mapper.web.TownMapepr;
import org.kzwl.service.web.CuVolunteersUserService;
import org.kzwl.service.web.TownService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CuVolunteersUserServiceImpl extends ServiceImpl<CuVolunteersUserMapper, CuVolunteersUser> implements CuVolunteersUserService {

    @Autowired
    CuVolunteersSignMapper cuVolunteersSignMapper;
    @Autowired
    UserMapper userMapper;

    @Override
    public ResponseData<IPage<CuVolunteersUser>> selectPage(VolunteersUserQueryBO volunteersUserQueryBO) {
        Page<CuVolunteersUser> volunteersUserPage = this.baseMapper.selectPage(new Page<>(volunteersUserQueryBO.getCurrent(), volunteersUserQueryBO.getSize()), new QueryWrapper<CuVolunteersUser>().lambda().like(!"".equals(volunteersUserQueryBO), CuVolunteersUser::getCuVolunteersName, volunteersUserQueryBO.getCuVolunteersName()));
        return new ResponseData<>(volunteersUserPage);
    }


    @Override
    public ResponseData<List<CuVolunteersSignDTO>> queryCuVolunteersSignUser(Integer cuVolunteersId) {
        List<CuVolunteersSignDTO> cuVolunteersSignDTOS = new ArrayList<>();
        List<CuVolunteersSign> cuVolunteersSignList = this.cuVolunteersSignMapper.selectList(new LambdaQueryWrapper<CuVolunteersSign>().eq(CuVolunteersSign::getCuVolunteersId, cuVolunteersId).eq(CuVolunteersSign::getCuSignState,1));
        if (cuVolunteersSignList.size() != 0) {
            cuVolunteersSignList.forEach(item -> {
                CuVolunteersSignDTO cuVolunteersSignDTO = new CuVolunteersSignDTO();
                User user = this.userMapper.selectById(item.getCuUserId());
                cuVolunteersSignDTO.setCuUsername(user.getCuUsername());
                cuVolunteersSignDTO.setCuSignExamineDate(item.getCuSignExamineDate());
                cuVolunteersSignDTO.setCuUserId(user.getCuUserId());
                cuVolunteersSignDTOS.add(cuVolunteersSignDTO);
            });
        }
        return new ResponseData<>(cuVolunteersSignDTOS);
    }
}
